package com.thread.concurrentTest;

import org.junit.Test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

public class ConcurrentListTest {

    @Test
    public void test0() {
        ReentrantLock lock = new ReentrantLock(); // 默认是非公平锁
        lock.lock();
        System.out.println("aaa");
        lock.unlock();
    }

    @Test
    public void test1() {
        ArrayList<Object> list = new ArrayList<>(); // 线程不安全
//        Vector<Object> list = new Vector<>(); // 线程安全
//        List<Object> list = Collections.synchronizedList(new ArrayList<>()); // 线程安全
//        CopyOnWriteArrayList<Object> list = new CopyOnWriteArrayList<>(); // 线程安全

        for (int i = 0; i < 300; ++i) {
            new Thread(() -> {
                list.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(list);
            }).start();
        }
    }

    @Test
    public void test2() {
//        HashSet<Object> set = new HashSet<>(); // 线程不安全
        CopyOnWriteArraySet<Object> set = new CopyOnWriteArraySet<>(); // 线程安全

        for (int i = 0; i < 300; ++i) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }).start();
        }
    }

    @Test
    public void test3() {
        HashMap<Object, Object> map = new HashMap<>(); // 线程不安全
//        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>(); // 线程安全

        for (int i = 0; i < 300; ++i) {
            String key = String.valueOf(i);
            new Thread(() -> {
                map.put(key, UUID.randomUUID().toString().substring(0, 8));
                System.out.println(map);
            }).start();
        }
    }

    @Test
    public void test4() {
        Object a = new Object();
        Object b = new Object();
        new Thread(() -> {
            synchronized (a) {
                System.out.println("已经获取锁a, 现在要拿锁b");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    System.out.println("已经获取锁b");
                }
            }
        }, "AA").start();
        new Thread(() -> {
            synchronized (b) {
                System.out.println("已经获取锁b, 现在要拿锁a");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a) {
                    System.out.println("已经获取锁a");
                }
            }
        }, "BB").start();
    }


}
